<?php
class AppController extends Controller {

    var $components = array('Acl', 'Auth', 'Session', 'Access');
    var $helpers = array('Html', 'Form', 'Session', 'Javascript');

    function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->authorize = 'actions';        
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'convocatorias', 'action' => 'publicaciones');
        $this->Auth->loginRedirect = array('controller' => 'convocatorias', 'action' => 'publicaciones');             
        $acceso = $this->Session->read('Auth.User');       
        $this->Auth->actionPath = 'controllers/';    
        $this->Auth->allowedActions = array('*');
    }
    
    function beforeRender() {
    	foreach($this->modelNames as $model) {
	      foreach($this->$model->_schema as $var => $field) {
	      	if(strpos($field['type'], 'enum') === FALSE)
	      	continue;
	
	      	preg_match_all("/\'([^\']+)\'/", $field['type'], $strEnum);
	
	      	if(is_array($strEnum[1])) {
	      		$varName = Inflector::camelize(Inflector::pluralize($var));
	      		$varName[0] = strtolower($varName[0]);
	      		$this->set($varName, array_combine($strEnum[1], $strEnum[1]));
	      	}
	      }
    	}
    }
    
    /**
     * uploads files to the server
     * @params:
     *		$folder 	= the folder to upload the files e.g. 'img/files'
     *		$formdata 	= the array containing the form files
     *		$itemId 	= id of the item (optional) will create a new sub folder
     * @return:
     *		will return an array with the success of each file upload
     */
    function uploadFiles($folder, $formdata, $itemId = null) {
    	// setup dir names absolute and relative
    	$folder_url = WWW_ROOT.$folder;
    	$rel_url = $folder;

    	// create the folder if it does not exist
    	if(!is_dir($folder_url)) {
    		mkdir($folder_url);
    	}

    	// if itemId is set create an item folder
    	if($itemId) {
    		// set new absolute folder
    		$folder_url = WWW_ROOT.$folder.'/'.$itemId;
    		// set new relative folder
    		$rel_url = $folder.'/'.$itemId;
    		// create directory
    		if(!is_dir($folder_url)) {
    			mkdir($folder_url);
    		}
    	}

    	// list of permitted file types, this is only images but documents can be added
    	$permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');

    	// loop through and deal with the files
    	foreach($formdata as $file) {
    		// replace spaces with underscores
    		$filename = str_replace(' ', '_', $file['name']);
    		// assume filetype is false
    		$typeOK = false;
    		// check filetype is ok
    		foreach($permitted as $type) {
    			if($type == $file['type']) {
    				$typeOK = true;
    				break;
    			}
    		}

    		// if file type ok upload the file
    		if($typeOK) {
    			// switch based on error code
    			switch($file['error']) {
    				case 0:
    					// check filename already exists
    					if(!file_exists($folder_url.'/'.$filename)) {
    						// create full filename
    						$full_url = $folder_url.'/'.$filename;
    						$url = $rel_url.'/'.$filename;
    						// upload the file
    						$success = move_uploaded_file($file['tmp_name'], $url);
    					} else {
    						// create unique filename and upload file
    						ini_set('date.timezone', 'Europe/London');
    						$now = date('Y-m-d-His');
    						$full_url = $folder_url.'/'.$now.$filename;
    						$url = $rel_url.'/'.$now.$filename;
    						$success = move_uploaded_file($file['tmp_name'], $url);
    					}
    					// if upload was successful
    					if($success) {
    						// save the url of the file
    						$result['urls'][] = $url;
    					} else {
    						$result['errors'][] = "Error uploaded $filename. Please try again.";
    					}
    					break;
    				case 3:
    					// an error occured
    					$result['errors'][] = "Error uploading $filename. Please try again.";
    					break;
    				default:
    					// an error occured
    					$result['errors'][] = "System error uploading $filename. Contact webmaster.";
    					break;
    			}
    		} elseif($file['error'] == 4) {
    			// no file was selected for upload
    			$result['nofiles'][] = "No file Selected";
    		} else {
    			// unacceptable file type
    			$result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";
    		}
    	}
    	return $result;
    }
}
?>